#include "oop4_2DArrayCpp.h"
Elem_TD::Elem_TD(size_t Size,void (*pFunction)(void* elem))
{
	ItsSizeOfElement=Size;
	pItsFunction=pFunction;
}
size_t Elem_TD:: getSize()
{
	return ItsSizeOfElement;
}
Elem_TD & Elem_TD::operator=(Elem_TD& rhs)
{
	if(this==&rhs)
		return *this;
	else
	{
		this->ItsSizeOfElement=rhs.getSize();
		this->pItsFunction=rhs.pItsFunction;
		return *this;
	}
}

Array::Array(int length, int width, Elem_TD elem)
{
	ItsLength=length;
	ItsWidth=width;
	ItsElement=elem;
	ItsMass=(void **)malloc( (ItsLength)*sizeof(void*) );
	for(int i=0;i<length;i++)
	{
		(ItsMass)[i]=malloc( ItsElement.getSize()*(ItsWidth) );
	
	}
}
Array::~Array()
{	
	for(int i=0;i<ItsLength;i++)
	{
		free(ItsMass[i]);
	}
	free(ItsMass);
}
void** Array::getMass()
{
	return this->ItsMass;
}
int Array::getLength()
{
	return this->ItsLength;
}
int Array::getWidth()
{
	return this->ItsWidth;
}
Elem_TD Array::getType()
{
	return this->ItsElement;
}
void* Array::getElement(int lengthCoord, int widthCoord)
{
	char * t = (char *)(*(this->getMass()+lengthCoord));
	Elem_TD Elem=this->getType();
	return( (void *)(t+widthCoord*Elem.getSize())); 
}
void Array::setElement(int x, int y, void* InputObject)
{
	void* PlaceForInput=this->getElement(x,y);
	memcpy(PlaceForInput, InputObject,this->getType().getSize());
}
void Array::DrawArray()
{
	void* current;
	for(int i=0;i<this->getLength();i++)
	{
		for(int j=0;j<this->getWidth();j++)
		{
			current=this->getElement(i,j);
			this->getType().pItsFunction(current);
		}
		cout<<endl;
	}
}
void DrawChar(void* elem)
{
	char*symb=(char*)elem;
	cout<<*symb;
}
void DrawDouble(void* elem)
{
	double*numb=(double*)elem;
	cout<<*numb;
}
void DrawInt(void* elem)
{
	int*numb=(int*)elem;
	cout<<*numb;
}